package com.example.module_car.adapter

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.example.lib_common.utils.ToastUtils
import com.example.module_car.R
import com.example.module_car.databinding.ItemCarBinding
import com.example.module_car.model.AddCarEntity
import com.example.module_car.model.CarEntityItem
import com.example.module_car.ui.edit.EditActivity

class CarAdapter : RecyclerView.Adapter<CarAdapter.ViewHolder>() {
    val mList = mutableListOf<CarEntityItem>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCarBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.post = mList[position]
        holder.binding.deleteButton.setOnClickListener {
            showCustomCenterDialog(holder.binding.deleteButton.context,position)
        }
        holder.binding.editButton.setOnClickListener {
            val it = mList[position]
            val intent = Intent(holder.binding.editButton.context,EditActivity::class.java)
                .putExtra("car_item",AddCarEntity(it.id,it.licensePlate,it.ownerName,it.provinceAbbreviation,it.userId))
            startActivity(intent)
        }
    }

    fun setList(listItem : MutableList<CarEntityItem>){
        mList.clear()
        mList.addAll(listItem)
        notifyDataSetChanged()
    }
    fun addListItem(item : CarEntityItem){
        mList.clear()
        mList.add(item)
        notifyDataSetChanged()
    }

    fun clearList(){
        mList.clear()
        notifyDataSetChanged()
    }

    inner class ViewHolder(val binding : ItemCarBinding) : RecyclerView.ViewHolder(binding.root)


    fun showCustomCenterDialog(context: Context, position: Int) {
        val dialog = Dialog(context).apply {
            setContentView(R.layout.dialog_delete_car)
            //点击外面不会消失
            setCanceledOnTouchOutside(false)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            // 设置点击事件
            findViewById<Button>(R.id.btnPositive).setOnClickListener {
                if (position != -1){
                    mList.removeAt(position)
                    notifyItemRemoved(position)
                    ToastUtils.showShort(context,"删除成功")
                }
                dismiss()
            }
            findViewById<Button>(R.id.btnNegative).setOnClickListener {
                dismiss()
            }
        }

        // 设置弹窗宽度
        dialog.window?.setLayout(
            (Resources.getSystem().displayMetrics.widthPixels * 0.9).toInt(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        dialog.show()
    }
}

